﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Senparc.Weixin.MP.AdvancedAPIs;
using Senparc.Weixin.MP.AdvancedAPIs.TemplateMessage;

namespace MxWeiXinPF.WeiXinComm
{
    public class WeiXinSendMessageComm
    {
        // Fields
        private WeiXinCRMComm cpp = new WeiXinCRMComm();
        private MxWeiXinPF.BLL.users daladd = new MxWeiXinPF.BLL.users();

        // Methods
        public string SendMessageGouYaoTiXin(string openid, int wid, string nickname, string content, string productname, string url)
        {
            string templateId = "VFku09to2IYiyxXGKGwT8hVtHuPF-RWzoUA3Kr_ooZc";
            string v = string.Empty;
            if (!string.IsNullOrEmpty(content))
            {
                v = content;
            }
            else
            {
                v = "尊敬的" + nickname + " 您的药品快要吃完了，要记得及时购买哦！";
            }
            string error = "";
           // string accessTokenOrAppId = this.cpp.getAccessToken(wid, out error);
            if (error != "")
            {
                return error;
            }
            TemplateDataSubscribe data = new TemplateDataSubscribe
            {
                first = new TemplateDataItem(v, "#173177"),
                keyword1 = new TemplateDataItem(nickname, "#173177"),
                keyword2 = new TemplateDataItem(productname, "#173177"),
                remark = new TemplateDataItem("敬请关注！", "#173177")
            };
            return TemplateApi.SendTemplateMessage(null, openid, templateId, "#FF0000", url, data, 0x2710).errcode.ToString();
        }

        public string SendMessageShouHuo(string type, string openid, int wid, decimal amount, decimal sumamount, string pnick_name, string nick_name, string url)
        {
            string templateId = "5dSrCSq9O4VrIvVvnyRimSk4K5FpsMampjWSwxOcI0w";
            string v = "增加";
            if (type.Equals("2"))
            {
                v = "减少";
            }
            string str3 = "";
            if (!pnick_name.Equals(""))
            {
                if (!nick_name.Equals(""))
                {
                    str3 = "您邀请的“" + pnick_name + "”的下级“" + nick_name + "”交易成功";
                }
                else
                {
                    str3 = "您邀请的“" + pnick_name + "”交易成功";
                }
            }
            else
            {
                str3 = "交易成功";
            }
            string error = "";
           // string accessTokenOrAppId = this.cpp.getAccessToken(wid, out error);
            if (error != "")
            {
                return error;
            }
            TemplateDataSubscribe data = new TemplateDataSubscribe
            {
                first = new TemplateDataItem(str3, "#173177"),
                FieldNam = new TemplateDataItem("发生时间", "#173177"),
                Account = new TemplateDataItem(DateTime.Now.ToString(), "#173177"),
                change = new TemplateDataItem(v, "#173177"),
                CreditChange = new TemplateDataItem(amount.ToString(), "#173177"),
                CreditTotal = new TemplateDataItem(sumamount.ToString(), "#173177"),
                remark = new TemplateDataItem("敬请关注！", "#173177")
            };
            try
            {
                return TemplateApi.SendTemplateMessage(null, openid, templateId, "#FF0000", url, data, 0x2710).errcode.ToString();
            }
            catch
            {
            }
            return "";
        }

        public string SendMessageSubscribe(string openid, int wid, string copenid, string url)
        {
            string nickName = this.daladd.GetNickName(copenid);
            string v = this.daladd.GetNickName(openid);
            string templateId = "WUEEL-7UZ0Hk4waD5igoER2camXo0Tko39xBP1zoips";
            string error = "";
            //string accessTokenOrAppId = this.cpp.getAccessToken(wid, out error);
            if (error != "")
            {
                return error;
            }
            TemplateDataSubscribe data = new TemplateDataSubscribe
            {
                first = new TemplateDataItem("尊敬的" + v, "#173177"),
                keyword1 = new TemplateDataItem(nickName, "#173177"),
                keyword2 = new TemplateDataItem(DateTime.Now.ToString(), "#173177"),
                keyword3 = new TemplateDataItem(v, "#173177"),
                remark = new TemplateDataItem("已经受邀关注！", "#173177")
            };
            return TemplateApi.SendTemplateMessage(null, openid, templateId, "#FF0000", url, data, 0x2710).errcode.ToString();
        }

        //用户提交了处方后,通知店铺管理者尽快处理审核通过
        public string SendMessageAddPrescription(string openid, int wid, string nickname, string content, string productname,string url)
        {
            string templateId = "VFku09to2IYiyxXGKGwT8hVtHuPF-RWzoUA3Kr_ooZc";
            string v = string.Empty;
            if (!string.IsNullOrEmpty(content))
            {
                v = content;
            }
            else
            {
                v = "亲,客户:" + nickname + " 提交一张新处方,请尽快审核！";
            }
            string error = "";
            string accessTokenOrAppId = this.cpp.getAccessToken(wid, out error);
            if (error != "")
            {
                return error;
            }
            TemplateDataSubscribe data = new TemplateDataSubscribe
            {
                first = new TemplateDataItem(v, "#173177"),
                keyword1 = new TemplateDataItem(nickname, "#173177"),
                keyword2 = new TemplateDataItem(productname, "#173177"),
                remark = new TemplateDataItem("敬请关注！", "#173177")
            };
            return TemplateApi.SendTemplateMessage(accessTokenOrAppId, openid, templateId, "#FF0000", url, data, 0x2710).errcode.ToString();
        }
        // Nested Types
        public class TemplateDataSubscribe
        {
            // Properties
            public TemplateDataItem Account { get; set; }

            public TemplateDataItem change { get; set; }

            public TemplateDataItem CreditChange { get; set; }

            public TemplateDataItem CreditTotal { get; set; }

            public TemplateDataItem FieldNam { get; set; }

            public TemplateDataItem first { get; set; }

            public TemplateDataItem keyword1 { get; set; }

            public TemplateDataItem keyword2 { get; set; }

            public TemplateDataItem keyword3 { get; set; }

            public TemplateDataItem remark { get; set; }
        }
    }


}
